//package com.wx.springboot.step9back.service.impl;
//
//import com.wx.springboot.step9back.common.api.CommonResult;
//import com.wx.springboot.step9back.esmodel.EsUser;
//import com.wx.springboot.step9back.mapper.EsUserMapper;
//import com.wx.springboot.step9back.repository.LoginInfoRepository;
//import com.wx.springboot.step9back.service.EsUserService;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
//import org.springframework.data.elasticsearch.core.SearchHit;
//import org.springframework.data.elasticsearch.core.SearchHits;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//import org.springframework.data.elasticsearch.core.query.Query;
//import org.springframework.stereotype.Service;
//
//import java.lang.reflect.Field;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
//
//@Service
//public class EsUserServiceImpl implements EsUserService {
//
//
//    @Autowired
//    private EsUserMapper esUserMapper;
//    @Autowired
//    private LoginInfoRepository esUserRepository;
//    @Autowired
//    private ElasticsearchRestTemplate elasticsearchRestTemplate;
//
//    @Override
//    public CommonResult importAllUserToEs() {
//        List<EsUser> esUsers = esUserMapper.selectPermissionList();
//        esUserRepository.saveAll(esUsers);
//        return CommonResult.success(esUsers.size());
//    }
//
//
//    @Override
//    public CommonResult searchHit(EsUser esUser) {
//        Query query = new NativeSearchQueryBuilder()
//                .withQuery(matchQuery("name", esUser.getName()))
//                .withHighlightFields(
//                        new HighlightBuilder.Field("name").preTags("<font color='red'>").postTags("</font>")
//                )
//                .build();
//        SearchHits<EsUser> search = elasticsearchRestTemplate.search(query, EsUser.class);
//        List<EsUser> collect = SearchHitsToEsWxPermissionList(search);
//        return CommonResult.success(collect);
//    }
//
//    private List<EsUser> SearchHitsToEsWxPermissionList(SearchHits<EsUser> search) {
//        List<SearchHit<EsUser>> searchHits = search.getSearchHits();
//        List<EsUser> collect = searchHits.stream().map(searchHit -> {
//            EsUser content = searchHit.getContent();
//            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
//            highlightFields.forEach((k, v) -> {
//                StringBuffer temp = new StringBuffer();
//                for (int i = 0; i < v.size(); i++) {
//                    temp.append(v.get(i));
//                }
//                String s = temp.toString();
//                try {
//                    Field keyField = EsUser.class.getDeclaredField(k);
//                    keyField.setAccessible(true);
//                    keyField.set(content, s);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            });
//            return content;
//        }).collect(Collectors.toList());
//        return collect;
//    }
//}
